///////////////////////////////////////////////////
// Election Administration Polarization
// Partisan Balance RD Plot
///////////////////////////////////////////////////

gl path "~/Library/CloudStorage/GoogleDrive-danmckinleythompson@gmail.com/My Drive/ElecAdminPolarization/How_Partisan_Is_Local_Election_Admin_Replication"

* Bring in the presidential election analysis data
use "$path/analysis_data/rd_election_analysis_data.dta", clear
keep if office=="pres"

* Loop over different definitions of partisan balance and save the RD estimate using that definition
matrix B = J(25, 6, .)
qui forval c = 2/25 {
	gen safe = abs(lag_vs_dem - 0.5)>(`c'/100) if lag_vs_dem!=.	
	rdrobust r_oos_state_year_vs_dem rv if safe==0, vce(cluster election_id)
	matrix B[`c', 1] = e(tau_cl)
	matrix B[`c', 2] = e(tau_cl) - 1.96*e(se_tau_cl)
	matrix B[`c', 3] = e(tau_cl) + 1.96*e(se_tau_cl)
	rdrobust r_oos_state_year_vs_dem rv if safe==1, vce(cluster election_id)
	matrix B[`c', 4] = e(tau_cl)
	matrix B[`c', 5] = e(tau_cl) - 1.96*e(se_tau_cl)
	matrix B[`c', 6] = e(tau_cl) + 1.96*e(se_tau_cl)	
	drop safe
}

* Plot the RD estimates across partisan balance definitions
svmat B
rename (B1-B6) (coef0 upper0 lower0 coef1 upper1 lower1)
gen cutpoint = 2*_n/100
twoway (connected coef0 cutpoint if cutpoint<=.5, mc(gs2) lc(gs2)) ///
	(line lower0 cutpoint if cutpoint<=.5, lc(gs10) lp(dash)) ///
	(line upper0 cutpoint if cutpoint<=.5, lc(gs10) lp(dash)), ///
	xti("Balance Cut Point") ///
	yti("Effect on Dem Pres Vote Share") yline(0) ///
	graphregion(color(white)) legend(off)
graph export "$path/output/rd_dem_pres_vs_balance.pdf", replace
